//
//  MilitaryViewController.m
//  One Day
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTMilitaryViewController.h"
#import "WTRecommendMainViewController.h"
#import "WTHotMainViewController.h"
#import "WTRecomendDetailViewController.h"
#import "WTPictureMainViewController.h"
#import "WTPictureDetailViewController.h"
#import "WTVideoMainViewController.h"
#import "WTCommunityViewController.h"
#import "WTLoginViewController.h"
#import "WTInformationHandle.h"
#import <UIButton+WebCache.h>



#define kSegmentHeight 50

@interface WTMilitaryViewController ()<UIScrollViewDelegate>

// 创建一个segment, 里面存放的是推荐,热点等,子界面按钮
@property (nonatomic, strong) UISegmentedControl *segment;

// 这个scrollView是整体的scrollView,segment对应Controller的这些都可以在scrollView上面滑动
@property (nonatomic, strong) UIScrollView *scrollView;

// 用来存放segment的Items的数组
@property (nonatomic, strong) NSArray *itemsArray;

// 推荐
@property (nonatomic, strong) WTRecommendMainViewController *recommendVC;

// 图库的控制器
@property (nonatomic, strong) WTPictureMainViewController * pictureVC;

// 视频的控制器
@property (nonatomic, strong) WTVideoMainViewController * videoVC;

@end

@implementation WTMilitaryViewController


- (void)viewDidAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    
     NSUserDefaults * user  = [NSUserDefaults standardUserDefaults];
    [WTInformationHandle shareInformationHandle].userName = [user valueForKey:@"userName"];
     [WTInformationHandle shareInformationHandle].userImg  = [user valueForKey:@"userImg"];
    [WTInformationHandle shareInformationHandle].ID = [user valueForKey:@"uid"];
    
    if ([WTInformationHandle shareInformationHandle].userImg != nil) {
        
       // 登录状态下,自定义导航栏左侧的按钮
        UIButton * userImgButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        userImgButton.frame = CGRectMake(0, 0, 30, 30);
        userImgButton.layer.cornerRadius = 15;
        userImgButton.layer.masksToBounds = YES;
        [userImgButton sd_setBackgroundImageWithURL:[NSURL URLWithString:[WTInformationHandle shareInformationHandle].userImg] forState:(UIControlStateNormal) placeholderImage:[UIImage imageNamed:@"m-zanweitu"]];
        [userImgButton addTarget:self action:@selector(loginButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
        UIBarButtonItem * leftButton = [[UIBarButtonItem alloc]initWithCustomView:userImgButton];
        self.navigationItem.leftBarButtonItem = leftButton;

    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // 视图已经加载完成,让其出现tabBar, 因为前面在跳转到图片详情的时候会隐藏导航栏
    
    self.tabBarController.tabBar.hidden = NO;
 

    
    self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
    self.navigationItem.title = @"推荐";
    
    // 创建右侧按钮
    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithTitle:@"社区" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightButtonClick:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    // 登录
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"m-denglu"] style:(UIBarButtonItemStylePlain) target:self action:@selector(loginButtonClick:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    
    // 初始化数组
    _itemsArray = @[@"推荐",@"热点",@"军事",@"历史",@"环球",@"视频",@"图片"];
    
    // 初始化segment
    _segment = [[UISegmentedControl alloc]initWithItems:_itemsArray];
    _segment.backgroundColor = [UIColor whiteColor];
    _segment.frame = CGRectMake(0, 64, kScreenWidth, kSegmentHeight * kRelativelength_Y);
    _segment.selectedSegmentIndex = 0;
    [_segment addTarget:self action:@selector(segmentControlAction:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:_segment];
    
    // 去掉segment的边框颜色
    _segment.tintColor = [UIColor clearColor];
    // 创建被选中的下标下的文字的字典
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor blueColor]};
    // 设置文字属性
    [_segment setTitleTextAttributes:selectedTextAttributes forState:(UIControlStateSelected)];
    
    // 设置没被选中的整体的属性
    NSDictionary *unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor lightGrayColor]};
    [_segment setTitleTextAttributes:unselectedTextAttributes forState:(UIControlStateNormal)];
  
    
    [self addScrollView];
    
    
    
    
    // Do any additional setup after loading the view.
}

// 点击右侧按钮跳转到社区
- (void)rightButtonClick:(UIBarButtonItem *)button
{
    WTCommunityViewController * communityVC = [[WTCommunityViewController alloc]init];
    [self.navigationController pushViewController:communityVC animated:YES];
}
#pragma mark -- 登录按钮
- (void)loginButtonClick:(UIBarButtonItem *)button
{
    WTLoginViewController * loginVC = [[WTLoginViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
    
}


#pragma mark --  添加大的scrollView,并且在这里面添加Controller
- (void)addScrollView
{
    // 初始化scrollView,高度是屏幕高度 - tabBar的49 - segment
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.segment.bottom, kScreenWidth, kScreenHeight - self.segment.bottom - 49)];
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.alwaysBounceVertical = NO;
    _scrollView.alwaysBounceHorizontal = YES;
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 7, _scrollView.height);

    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    [self addSubController];
    
}

#pragma mark -- 添加控制器
- (void)addSubController
{
    // 推荐的
    _recommendVC = [[WTRecommendMainViewController alloc]init];
    _recommendVC.view.frame = CGRectMake(0, 0, _scrollView.width, _scrollView.height);
    [_scrollView addSubview:_recommendVC.view];
    
 
    // 之所以在这么写block方法,是因为WTRecommendMainViewController没有导航栏,不能够push
    __block WTMilitaryViewController *mySelf = self;
    _recommendVC.block = ^(NSString *ID, NSString * commentNum){
    WTRecomendDetailViewController *detail = [[WTRecomendDetailViewController alloc]init];
       // 把ID传给下一个页面
        detail.ID = ID;
        detail.commentNumber = commentNum;
    [mySelf.navigationController pushViewController:detail animated:YES];
      
    };
    
    
#pragma mark  -- 创建4个controllView 对应热点,军事,历史,环球
    for (int i = 2; i <= 5; i++) {
        
     WTHotMainViewController * hotVC = [[WTHotMainViewController alloc]init];
     hotVC.view.frame = CGRectMake(kScreenWidth * (i - 1), 0, _scrollView.width , _scrollView.height);
     [_scrollView addSubview:hotVC.view];
     [self addChildViewController:hotVC];
        
    // 之所以在这么写block方法,是因为WTHotMainViewController没有导航栏,不能够push
 
    hotVC.block = ^(NSString *ID, NSString * commentNum){
        WTRecomendDetailViewController *detail = [[WTRecomendDetailViewController alloc]init];
        // 把ID传给下一个页面
        detail.ID = ID;
        detail.commentNumber =  commentNum;
        [mySelf.navigationController pushViewController:detail animated:YES];
    };
        
    }

#pragma mark -- 视频的控制器
    _videoVC = [[WTVideoMainViewController alloc]init];
    _videoVC.view.frame = CGRectMake(kScreenWidth * 5, 0, kScreenWidth, _scrollView.height);
    [_scrollView addSubview:_videoVC.view];
    
    _videoVC.block = ^(NSString * ID, NSString * commentNum){
        WTRecomendDetailViewController *detail = [[WTRecomendDetailViewController alloc]init];
        // 把ID传给下一个页面
        detail.ID = ID;
        detail.commentNumber =  commentNum;
        [mySelf.navigationController pushViewController:detail animated:YES];
    
    };
    
 
    
#pragma mark -- 图片的控制器
    _pictureVC = [[WTPictureMainViewController alloc]init];
    _pictureVC.view.frame = CGRectMake(kScreenWidth * 6 , 0, kScreenWidth , _scrollView.height + 64);
    [_scrollView addSubview:_pictureVC.view];
    
    
    _pictureVC.block = ^(NSString * ID, NSString * title){
    
        WTPictureDetailViewController * detailVC = [[WTPictureDetailViewController alloc]init];
   
        detailVC.ID = ID;
        detailVC.detailTitle = title;
        [mySelf.navigationController pushViewController:detailVC animated:YES];
    };
    
}
#pragma mark -- segment的改变触发的方法
- (void)segmentControlAction:(UISegmentedControl *)segment
{
    // 实现文字的动画效果
    [UIView beginAnimations:@"donghua" context:nil];
    [UIView  setAnimationDuration:3];
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor blueColor]};
    [segment setTitleTextAttributes:selectedTextAttributes forState:(UIControlStateSelected)];
    [UIView commitAnimations];
    
    // 标题
    self.navigationItem.title = _itemsArray[segment.selectedSegmentIndex];
    // scrollView偏移到相应位置
    [_scrollView setContentOffset:CGPointMake(kScreenWidth * _segment.selectedSegmentIndex, 0)];
    
    
#pragma mark -- 取出对应下标的WTHotMainViewController, 让它刷新一下
    if (segment.selectedSegmentIndex > 0 && segment.selectedSegmentIndex <= 4) {
        WTHotMainViewController *hotVC = (WTHotMainViewController *)self.childViewControllers[segment.selectedSegmentIndex - 1];
        
        NSString *list = [NSString stringWithFormat:@"%ld", segment.selectedSegmentIndex + 1];
        [hotVC requestData:list];
    }
   
    if (segment.selectedSegmentIndex == 6) {
        [_pictureVC requestData];
    } else if(segment.selectedSegmentIndex == 5){
        
        [_videoVC requestData];
    }
   
}

#pragma mark -- scrollView的方法,scrollView滑动结束的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // scrollView结束后取出当前的偏移量,然后得出目前对应segment的第几个
    CGFloat x = scrollView.contentOffset.x;
    NSInteger index = x / kScreenWidth;
    _segment.selectedSegmentIndex = index;
    
    // 调用该方法,实现segment选中的文字有动画效果
    [self segmentControlAction:_segment];
    
    self.navigationItem.title = _itemsArray[index];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
